package com.chenyongjing.design.pattern.singleton.LazyStaticInnerClassSingleton;

/**
 * 采用静态内部类,实现懒汉式写法
 * ClassPath：LazystaticInnerClassSingleton。class 默认加载
 * LazystaticInnerClassSingleton$LazyHolder 用到之后再加载
 * 优点：写法优雅，利用Java本身的语法特点，性能高，并避免浪费内存
 * 缺点：能够被反射破坏
 */
public class LazystaticInnerClassSingleton {
    private LazystaticInnerClassSingleton(){
        // 处理反射破坏的问题
        if(LazyHolder.INSTANCE != null){
            throw new RuntimeException("不允许非法访问");
        }

    }

    private LazystaticInnerClassSingleton getInstance(){
        return LazyHolder.INSTANCE;
    }

    private static class LazyHolder{
        private static final LazystaticInnerClassSingleton INSTANCE = new LazystaticInnerClassSingleton();
    }

}
